<?php
/**
 * OS CRM SYSTEM - An open source application development framework for PHP 4.3.2 or newer
 *
 * CLASS - CATEGORY - OS Admin
 * 
 * @name      OS Admin
 * @package		OS CRM
 * @category  Class
 * @author		CRM SYSTEM Dev Team
 * @copyright	Copyright (c) 2010 - 2012, CRM SYSTEM
 * @since     Version 1.0
 * @filesource
 * @access    public 
 */

// ------------------------------------------------------------------------

class os_admin extends Controller {
  
  /*common class varibale*/
  var $c 	= 'os_admin';//current controler
	var $p_c = 'category';//parent controller
  var $cur_c = 'category/os_admin';//current pasth controller
  
  //set db - listing data
	var $e_list_limit 	= '50';
	var $e_list_offset 	= '0';
	var $e_list_order   = 'asc';
	var $e_list_order_by= 'os_category_name'; 
  
  /*message strings*/
  var $valid_success 	= '';
  var $valid_errors 	= '';
  var $valid_notice 	= '';
  
  /**
   * os_admin  - constructor of the c;ass
   * 
   * @access public
   * @param  NA
   * @return void
   */
	function os_admin()
	{
		parent::Controller();	
    
    if(!__is_loggedin()){
      redirect(OS_CRM_SITEADMIN);
    }
    
    $this->lang->load('common', ADMIN_LANGUAGE);
	}
	
  /** 
	 * Index
	 *
	 * @desc NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function index()
	{
		$this->list_all();
	}
  
 
	/** 
	 * List All
	 *
	 * @descp List all the pages in the system
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
	function list_all()
	{
		$data = array();
		$data['cont_title'] = 'Category/Subjects Management System';
		
		//get all pages
		$result_set 		= $this->_generate_list();
		$data['list'] 		= $result_set['list'];
		$data['num_rows'] 	= $result_set['num_rows'];
		
    $data['frm_name']   = 'frmcatlist';
    $data['frm_action'] = site_url( $this->cur_c );
    
		//set crontroller info
		$data['c'] 		= $this->c;
		$data['p_c'] 	= $this->p_c;
    $data['cur_c'] 	= $this->cur_c;
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->session->flashdata('valid_errors');
    $data['msg_info']['valid_notice'] = $this->session->flashdata('valid_notice');
    $data['msg_info']['valid_success'] = $this->session->flashdata('valid_success');
    
		__admin_display('listing',$data);
	}
	  
	/** 
	 * Add - page
	 *
	 * @descp interface to add the page details
	 * @access public
	 * @param  na
	 * @return void
	 */
	function add()
	{
		$id = 0;
		$token = random_string();
    $post_arr = array();
    $post_arr['os_category_status']     = $this->input->post('os_category_status');;
    $post_arr['os_category_name']       = $this->input->post('os_category_name');
    $post_arr['os_category_descp']      = $this->input->post('os_category_descp');
    
		$data = array();
		$data['cont_title'] 	= 'Add new record';		
    $data['post_arr']     = $post_arr;
		
		/*set crontroller info*/
		$data['c'] 	= $this->c;
		$data['p_c'] 	= $this->p_c;
		$data['cur_c'] 	= $this->cur_c;
    
    /*set form actions*/
    $data['frm_name']   = 'frmjournaladd';
    $data['frm_action'] = site_url( $this->cur_c .'/insert');
    $data['frm_token']  = $token;
    $data['frm_mode']   = 'add';
    $data['frm_id']      = $id;
    
    $this->session->set_userdata('frm_token', $token);
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->valid_errors;
    $data['msg_info']['valid_notice'] = $this->valid_notice;
    $data['msg_info']['valid_success'] = $this->valid_success;
    
		__admin_display('add',$data);
	}
	
	/** 
	 * Edit - page
	 *
	 * @descp interface to edit the page details
	 * @access public
	 * @param  integer - page id	 
	 * @return void
	 */
	function edit($id = 0)
	{
		$id = (int) $id;
		
		if(!empty($id))
		{
			$token = random_string();
      $post_arr = array();
      
      if (!$_POST) {
        $qry = "SELECT  * 
                FROM " . TBL_CATEGORY . "
                WHERE os_category_id = '" . $id . "'
              ";
        $handle = $this->db->query($qry);
        $num_rows = $handle->num_rows();
        if ($num_rows > 0) {
          $post_arr = $handle->row_array();                          
        } else {
          redirect($this->cur_c);
        }
      } else {
        $post_arr = $this->_set_post_data();        
      }
      
      $data = array();
      $data['cont_title'] 	= 'Edit Record ID: '.$id;
      $data['post_arr']     = $post_arr;

      /*set crontroller info*/
      $data['c'] 	= $this->c;
      $data['p_c'] 	= $this->p_c;      
      $data['cur_c'] 	= $this->cur_c;
    
      /*set form actions*/
      $data['frm_name']   = 'frmcatedit';
      $data['frm_action'] = site_url( $this->cur_c .'/update');
      $data['frm_token']  = $token;
      $data['frm_mode']   = 'edit';
      $data['frm_id']      = $id;
      
      $this->session->set_userdata('frm_token', $token);
      /*set message strings*/
      $data['msg_info']['valid_errors'] = $this->valid_errors;
      $data['msg_info']['valid_notice'] = $this->valid_notice;
      $data['msg_info']['valid_success'] = $this->valid_success;
      
      __admin_display('add',$data);
		}
		else
		{
			redirect($this->cur_c);
		}
	}
	
	/** 
	 * Preview - page
	 *
	 * @descp interface to edit the page details
	 * @access public
	 * @param  integer - page id	 
	 * @return void
	 */
	function preview($id = 0)
	{
		$id = (int) $id;
		
		if(!empty($id))
		{
			$token = random_string();
      $post_arr = array();
      
      $qry = "SELECT  * 
              FROM " . TBL_CATEGORY . "
              WHERE os_category_id = '" . $id . "'
            ";
      $handle = $this->db->query($qry);
      $num_rows = $handle->num_rows();
      if ($num_rows > 0) {
        $post_arr = $handle->row_array();                          
      } else {
        redirect($this->cur_c);
      }
      
      $data = array();
      $data['cont_title'] 	= 'Preview Record ID: '.$id;
      $data['post_arr']     = $post_arr;

      /*set crontroller info*/
      $data['c'] 	= $this->c;
      $data['p_c'] 	= $this->p_c;

      /*set form actions*/
      $data['frm_name']   = 'frmcatedit';
      $data['frm_action'] = site_url( $this->cur_c);
      $data['frm_token']  = $token;
      $data['frm_mode']   = 'preview';
      $data['frm_id']      = $id;
      
      $this->session->set_userdata('frm_token', $token);
      /*set message strings*/
      $data['msg_info']['valid_errors'] = $this->valid_errors;
      $data['msg_info']['valid_notice'] = $this->valid_notice;
      $data['msg_info']['valid_success'] = $this->valid_success;
      
      __admin_display('preview',$data);
		}
		else
		{
			redirect($this->cur_c);
		}
	} 
	 
  
  /** 
	 * Insert
	 *
	 * @desc this will validate and insert a new record
	 * @access public
	 * @param  NA	 
	 * @return mixed
	 */
	function insert()
	{   
		$this->load->library('form_validation');
		$login_status     = false;
		$data             = array();
    $frm_token        = $this->input->post('frm_token');
		$sess_frm_token   = $this->session->userdata('frm_token');
    $this->session->unset_userdata('frm_token');
		$errors = $this->_val_form();     
		if(!empty($errors)){
			$this->valid_errors =  $errors;
      $this->add();
    }else if($frm_token != $sess_frm_token){      
			$this->valid_errors =  '<p>&raquo; Sorry form cannot be posted twice, please re-enter the details</p>';
      $this->add();
    }else{
      $data = array();      
      $status = $this->input->post('os_category_status');
      $status = (!empty($status)) ? '1':'0';     
      
      $data['os_category_status'] = $status;
      $data['os_category_name'] = $this->input->post('os_category_name');
      $data['os_category_descp'] = filter_data($this->input->post('os_category_descp'));
      $data['os_category_created'] = date(DB_DATE_FORMAT);
      $this->db->insert(TBL_CATEGORY, $data); 
      $last_id = $this->db->insert_id();
      //__debug($last_id);
      
      $msg = $this->lang->line('db_insert_success');
      $this->session->set_flashdata('valid_success', sprintf($msg, $last_id));
      //__debug($this->session->flashdata('valid_success'));
      redirect($this->cur_c. '/list_all');
    }
	}
  
  
  
  /** 
	 * Update
	 *
	 * @desc this will validate and update a record
	 * @access public
	 * @param  NA	 
	 * @return mixed
	 */
	function update()
	{   
		$this->load->library('form_validation');
		$login_status     = false;
		$data             = array();
    $frm_token        = $this->input->post('frm_token');
    $frm_id           = (int) $this->input->post('frm_id');
    
		$sess_frm_token   = $this->session->userdata('frm_token');
    $this->session->unset_userdata('frm_token');
    
		$errors = $this->_val_form();    
    if(empty($frm_id)){
      redirect($this->c);
    }else if(!empty($errors)){      
			$this->valid_errors =  $errors;
      $this->edit($frm_id);
    }else if($frm_token != $sess_frm_token){      
			$this->valid_errors =  '<p>&raquo; Sorry form cannot be posted twice, please re-enter the details</p>';
      $this->edit($frm_id);
    }else{
      $data = array();      
      
      $status = $this->input->post('os_category_status');
      $status = (!empty($status)) ? '1':'0';     
      
      $data['os_category_status'] = $status;
      $data['os_category_name'] = $this->input->post('os_category_name');
      $data['os_category_descp'] = filter_data($this->input->post('os_category_descp'));
      $data['os_category_modified'] = date(DB_DATE_FORMAT);
      
      $this->db->where('os_category_id', $frm_id);
      $this->db->update(TBL_CATEGORY, $data); 
      
      $msg = $this->lang->line('db_upd_success');
      $this->session->set_flashdata('valid_success', sprintf($msg, $frm_id));
      //__debug($this->session->flashdata('valid_success'));
      redirect($this->cur_c . '/list_all');
    }
	}
  
  /** 
 	 * General List
	 *
	 * @descp NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function _generate_list()
	{
		$return_arr  = array();
		
		$limit 		= $this->e_list_limit;
		$offset 	= $this->e_list_offset;
		$order 		= $this->e_list_order;
		$order_by = $this->e_list_order_by;
		
		$qry = "SELECT  c1.* , c2.os_category_name as os_category_parent_name
            FROM ".TBL_CATEGORY." c1
            LEFT OUTER JOIN ".TBL_CATEGORY." c2 ON c1.os_category_id = c2.os_category_parent
            ORDER BY  $order_by	$order
            LIMIT $offset  , $limit 							  			
            ";			
    //__debug($qry);exit;
		$return_arr = run_cust_qry($qry);
		return $return_arr;
	}
 

  
  /** 
	 * Validation Form
	 *
	 * @descp NA
	 * @access private
	 * @param  NA	 
	 * @return array
	 */
	function _val_form()
	{
		$config =  array();
    
		$config[] = array(
						 'field'   => 'os_category_name',
						 'label'   => 'subject/category',
						 'rules'   => 'trim|required|min_length[5]|xss_clean'
					  );
		$config[] =  array(
						 'field'   => 'os_category_descp',
						 'label'   => 'description',
						 'rules'   => 'trim|min_length[5]|xss_clean'
					  );
		$this->form_validation->set_rules($config); 
		$this->form_validation->set_error_delimiters('<p>&raquo; ', '</p>');
		if ($this->form_validation->run() == false)
		{
			return validation_errors(); 
		}
	}	
	
  /** 
	 * Validation Form
	 *
	 * @descp NA
	 * @access private
	 * @param  NA	 
	 * @return array
	 */
	function _set_post_data()
	{
    $var = array();    
    if(isset($_POST)){      
      foreach($_POST as $key => $val){
        $var[$key] = $this->input->post($key); 
      }
    }    
    return $var;
  }
  
}

/* End of file login.php */
/* Location: ./system/application/modules/login/controllers/login.php */